package com.hgvip.manuallyCommit;

import com.hgvip.common.KafkaParams;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

/**
 * Created by arnold.zhu on 2017/8/3.
 */
public class Consumer {

    private static Logger logger = LoggerFactory.getLogger(com.hgvip.autoCommit.Consumer.class);

    public static void main(String[] args) throws InterruptedException {
        Properties props = new Properties();
        props.put("bootstrap.servers", KafkaParams.BOOTSTRAP_SERVERS);
        props.put("group.id", KafkaParams.GROUP_ID);
        props.put("enable.auto.commit", "false");
        props.put("key.deserializer", KafkaParams.KEY_DESERIALIZER);
        props.put("value.deserializer", KafkaParams.VALUE_DESERIALIZER);
        KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
        // kafka将已订阅topic的消息发送到每个消费者组中。并通过平衡分区在消费者分组中所有成员之间来达到平均。
        consumer.subscribe(Collections.singletonList("my-topic23"));
        final int minBatchSize = 200;
        List<ConsumerRecord<String, String>> buffer = new ArrayList<>();
        while (true) {
            ConsumerRecords<String, String> records = consumer.poll(100);
            for (ConsumerRecord<String, String> record : records) {
                buffer.add(record);
            }
            if (buffer.size() >= minBatchSize) {
                // 在相应的记录插入数据库之后再手动提交offset。这样我们可以准确控制消息是成功消费的。
                insertIntoDb(buffer);
                consumer.commitSync();
                buffer.clear();
            }
        }
    }

    private static void insertIntoDb(List<ConsumerRecord<String, String>> buffer) {
        logger.info("插入数据库操作...");
    }

}
